﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Star.Tools.Attributes
{
    public static class DisplayAttributeHelper
    {

        public static List<string> GetDisplayName<T>()
        {
          
            List<string> listName = new List<string>();
            Type objType = typeof(T);
            //取属性上的自定义特性
            foreach (PropertyInfo propInfo in objType.GetProperties())
            {
                object[] objAttrs = propInfo.GetCustomAttributes(typeof(DisplayAttribute), true);
                if (objAttrs.Length > 0)
                {
                    DisplayAttribute attr = objAttrs[0] as DisplayAttribute;
                    if (attr != null)
                    {
                        listName.Add(attr.Name); 
                    }
                }
            }
            return listName;



        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static List<PropertyInfo> GetDisplayPropInfo(Type t)
        {
            PropertyInfo[] propArray = t.GetProperties();
            var orderedProps = propArray
                .Select(p => new { p, Atts = p.GetCustomAttributes(typeof(DisplayAttribute), inherit: true) })
                .Where(p => p.Atts.Length != 0)
                //.OrderBy(p => ((DisplayAttribute)p.Atts[0]).Order)
                .Select(p => p.p).OrderBy(c => c.MetadataToken)
                .ToList();
            var unOrderedProps = propArray
                .Select(p => new { p, Atts = p.GetCustomAttributes(typeof(DisplayAttribute), inherit: true) })
                .Where(p => p.Atts.Length == 0)
                .Select(p => p.p).OrderBy(c => c.MetadataToken)
                .ToList();
            List<PropertyInfo> properties = new List<PropertyInfo>();
            properties.AddRange(orderedProps);
            properties.AddRange(unOrderedProps);
            return properties;
        }

        /// <summary>
        /// 获取显示名称
        /// </summary>
        /// <param name="customAttributeProvider">自定义特性提供程序</param>
        /// <param name="inherit">是否继承</param>
        public static string GetDisplayName(this ICustomAttributeProvider customAttributeProvider, bool inherit = false)
        {
            string displayName = null;
            var displayAttribute = customAttributeProvider.GetAttribute<DisplayAttribute>();
            if (displayAttribute != null)
            {
                displayName = displayAttribute.Name;
            }
            else
            {
                var displayNameAttribute = customAttributeProvider.GetAttribute<DisplayNameAttribute>();
                if (displayNameAttribute != null)
                    displayName = displayNameAttribute.DisplayName;
            }
            return displayName;
        }
        /// <summary>
        /// 获取类型描述
        /// </summary>
        /// <param name="customAttributeProvider">自定义特性提供程序</param>
        /// <param name="inherit">是否继承</param>
        public static string GetDescription(this ICustomAttributeProvider customAttributeProvider, bool inherit = false)
        {
            var desc = string.Empty;
            var descAttribute = customAttributeProvider.GetAttribute<DescriptionAttribute>();
            if (descAttribute != null)
                desc = descAttribute.Description;
            return desc;
        }
        /// <summary>
        /// 获取特性
        /// </summary>
        /// <typeparam name="T">特性类型</typeparam>
        /// <param name="customAttributeProvider">自定义特性提供程序</param>
        /// <param name="inherit">是否继承</param>
        public static T GetAttribute<T>(this ICustomAttributeProvider customAttributeProvider, bool inherit = false)
            where T : Attribute =>
            customAttributeProvider
                .GetCustomAttributes(typeof(T), inherit)
                .OfType<T>()
                .FirstOrDefault();

        /// <summary>
        /// 获取特性列表
        /// </summary>
        /// <typeparam name="T">特性类型</typeparam>
        /// <param name="customAttributeProvider">自定义特性提供程序</param>
        /// <param name="inherit">是否继承</param>
        public static IEnumerable<T> GetAttributes<T>(this ICustomAttributeProvider customAttributeProvider,
            bool inherit = false) where T : Attribute =>
            customAttributeProvider
                .GetCustomAttributes(typeof(T), inherit)
                .OfType<T>();
    }
  
}
